//
//  ViewController1.m
//  BarCodeAndQRCode
//
//  Created by jiezsoft.com on 14-10-10.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.
//

#import "ScanViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "HAODEsingleton.h"
#import "RDVTabBarController.h"
#import "MembershipDetailsViewController.h"
#import "ProductDetailViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "kucunViewController.h"
#import "RDVTabBarController.h"
#import "GetProductDetailViewController.h"
#import "OrderDetailViewController.h"
@interface ScanViewController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
@property(nonatomic,strong)NSString* str;
@end

@implementation ScanViewController
{
    HAODEsingleton* commdity;
    double d;
    int d1;
    NSString*specName;
    UITextField*searchTextField;
    NSMutableArray* arrrs1;
    NSMutableArray* arrrs2;
    NSMutableArray* arrrs3;
    NSMutableArray* arrrs4;
    NSMutableArray* arrrs5;
    NSMutableArray* arrrs6;
    NSMutableArray* arrrs7;
    NSMutableArray* scanStockResultArray;
    NSMutableArray* itemUrlArray;
    NSMutableArray* itemTypeNameArray;
    NSMutableArray* itemSeriesNameArray;
    NSMutableArray* itemInDateArray;
    NSMutableArray* itemWeightArray;
    NSMutableArray* itemPriceArray;
    NSMutableArray* currentSpecIdArray;
    NSMutableArray* itemNameArray;
    NSMutableArray* itemidArray;
    NSMutableArray* itemCodeArray;
    NSMutableArray* howArray;
    
    NSMutableArray* specNameArray;
    UIButton*SCANSTOCK;
    UIButton *SCANSTOCK1;
    UIButton *shuSCANSTOCK1;
    BOOL no;
    UIButton*sxBtn;
    
    UIButton*jilu;
    UIButton*shangpin;
    UIButton*huiyuan;
    UIView*diview;
    UIView*diviewTming;
    UIButton*jilu1;
    UIButton* shangpin1;
    UIButton*huiyuan1;
    UIButton*editor1;
    UIButton* backButton;
    UILabel*titleStr;
    UIView* view;
    NSArray*nameBtn;
    NSArray *arrayimage;
    NSArray *arrayimage1;
}
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)BackAction:(UIButton *)sender{
    
    [_line removeFromSuperview];
     [[self rdv_tabBarController]setSelectedIndex:0];
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    [_line removeFromSuperview];
    
    
    self.navigationController.delegate = nil;
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    self.navigationItem.rightBarButtonItem = nil;
    self.navigationController.navigationBarHidden=YES;
    [[self rdv_tabBarController] setTabBarHidden:YES];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    

    
    commdity=[HAODEsingleton shared];

    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
  

    [shuSCANSTOCK1 removeFromSuperview];
    [SCANSTOCK1 removeFromSuperview];
    [SCANSTOCK removeFromSuperview];

    
    if ([commdity.sat isEqualToString:@"1"]) {

        if ([commdity.sx isEqualToString:@"sx"]) {
            
            
            
                            scanStockResultArray=[[NSMutableArray alloc]init];
                            itemUrlArray=[[NSMutableArray alloc]init];
                            itemPriceArray=[[NSMutableArray alloc]init];
                            itemNameArray=[[NSMutableArray alloc]init];
                            itemidArray=[[NSMutableArray alloc]init];
                            itemCodeArray=[[NSMutableArray alloc]init];
                            currentSpecIdArray=[[NSMutableArray alloc]init];
                            howArray=[[NSMutableArray alloc]init];
                            specNameArray=[[NSMutableArray alloc]init];
                            [howArray removeAllObjects];
                            [itemCodeArray removeAllObjects];
                            [currentSpecIdArray removeAllObjects];
                            [itemNameArray removeAllObjects];
                            [itemPriceArray removeAllObjects];
                            [itemidArray removeAllObjects];
                            [scanStockResultArray removeAllObjects];
                            [itemUrlArray removeAllObjects];
                            [specNameArray removeAllObjects];
                            itemCodeArray=[commdity.ItemCode44 mutableCopy];
                            scanStockResultArray=[commdity.Itemid44 mutableCopy];
                            itemUrlArray = [commdity.ImageUrl44 mutableCopy];
                            howArray = [commdity.how44 mutableCopy];
                            itemPriceArray = [commdity.Price44 mutableCopy];
                            currentSpecIdArray = [commdity.currentSpecId44 mutableCopy];
                            itemNameArray = [commdity.ItemName44 mutableCopy];
                            itemidArray = [commdity.Itemid44 mutableCopy];
                            specNameArray = [commdity.specName44 mutableCopy];
                            
                            NSNumber *sum = [howArray valueForKeyPath:@"@sum.floatValue"];

                            [shuSCANSTOCK1 setTitle: [NSString stringWithFormat:@"%@", sum] forState:UIControlStateNormal];
                            [shuSCANSTOCK1 addTarget:self action:@selector(scanStockCountBtnClick) forControlEvents:UIControlEventTouchUpInside];
                        }else{
            
                            NSNumber *sum = [howArray valueForKeyPath:@"@sum.floatValue"];
                            [shuSCANSTOCK1 setTitle: [NSString stringWithFormat:@"%@", sum] forState:UIControlStateNormal];
                            [shuSCANSTOCK1 addTarget:self action:@selector(scanStockCountBtnClick) forControlEvents:UIControlEventTouchUpInside];
                        }
        

        
    }
     [self add]; 
  
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
     commdity=[HAODEsingleton shared];
    commdity.sat=@"1";
    itemCodeArray=[[NSMutableArray alloc]init];
    scanStockResultArray=[[NSMutableArray alloc]init];
    itemUrlArray=[[NSMutableArray alloc]init];
    itemTypeNameArray=[[NSMutableArray alloc]init];
    itemSeriesNameArray=[[NSMutableArray alloc]init];
    itemInDateArray=[[NSMutableArray alloc]init];
    currentSpecIdArray=[[NSMutableArray alloc]init];
    itemPriceArray=[[NSMutableArray alloc]init];
    itemidArray=[[NSMutableArray alloc]init];
    itemNameArray=[[NSMutableArray alloc]init];
    howArray=[[NSMutableArray alloc]init];
    specNameArray=[[NSMutableArray alloc]init];
    commdity.sx=@"";
    nameBtn=[[NSArray alloc]initWithObjects:@"核销券",@"扫记录",@"扫订单",nil];
   arrayimage = @[@"hexiaoquan_normal",@"saojilv_normal",@"saodingdan_normal"];
   arrayimage1 = @[@"hexiaoquan_hight",@"saojilv_hight",@"saodingdan_hight"];

}

-(void)jiazaititle{
    [sxBtn removeFromSuperview];
    [backButton removeFromSuperview];
    [titleStr removeFromSuperview];
    [view removeFromSuperview];
     sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-45, 27.5, 30, 30)];
    [sxBtn setImage:[UIImage imageNamed:@"shanguangdeng_normal"] forState:UIControlStateNormal];
    [sxBtn addTarget:self action:@selector(sou) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sxBtn];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(12.5,27.5,30,30);
    backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];

    view=[[UIView alloc]initWithFrame:CGRectMake(45, 25, ScreenWidth-95, 35)];
    view.backgroundColor=zuiqianhuicolor;
    view.backgroundColor=[UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1];
    view.alpha=0.69;
    UIButton *sxBtn1 = [[UIButton alloc] initWithFrame:CGRectMake( 5, 5, 25, 25)];
    [sxBtn1 setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
    //[sxBtn1 addTarget:self action:@selector(sou) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:sxBtn1];
     [self.view addSubview:view];
    
    searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(sxBtn1.right,0, ScreenWidth*400/750,35)];
    [searchTextField setBorderStyle:   UITextBorderStyleNone]; //外框类型
    searchTextField.placeholder = @" 请输入产品编码"; //默认显示的字
    searchTextField.font = [UIFont systemFontOfSize: 14];
    searchTextField.tag=22;
    searchTextField.keyboardType = UIKeyboardTypeDecimalPad;
    searchTextField.returnKeyType = UIReturnKeyDone;
    searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    searchTextField.delegate = self;
    [view addSubview:searchTextField];
    
    UIView* myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:daxiao];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    searchTextField.inputAccessoryView = myToolBar;

 
}

-(void)add{
    
   
    [_viewPreview removeFromSuperview];
    [timer invalidate];
    stringValue=@"";
    UIBarButtonItem* barbuttonitem=[[UIBarButtonItem alloc]init];
    barbuttonitem.title=@"";
    barbuttonitem.tintColor=[UIColor whiteColor];
    [barbuttonitem setTintColor:[UIColor whiteColor]];
    self.navigationItem.backBarButtonItem=barbuttonitem;
    
    isDown=NO;
    num=0;
    _captureSession=nil;
    
   
    _viewPreview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    _viewPreview.frame = CGRectMake(0, 0,ScreenWidth,ScreenHeight);
    [self.view addSubview:_viewPreview ];
    
    [self startRead1];
    [self shuliang];
    [ self jiazaidi];
    UILabel* tishi=[[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight*380/1334,ScreenWidth,14)];
    tishi.text=@"对准条码或二维码，即可自动扫描";
    tishi.textColor=[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1]
;
    tishi.textAlignment=NSTextAlignmentCenter;
    tishi.font=[UIFont systemFontOfSize:14];
    NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:@"对准条码或二维码，即可自动扫描"];
    
    [AttributedStr1 addAttribute:NSForegroundColorAttributeName
     
                           value:luseColor
     
                           range:NSMakeRange(2, 2)];
    [AttributedStr1 addAttribute:NSForegroundColorAttributeName
     
                           value:luseColor
     
                           range:NSMakeRange(5, 3)];
    tishi.attributedText = AttributedStr1;

    [self.view addSubview:tishi];
    
    
    UIView* view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight*440/1334)];
    view1.backgroundColor=[UIColor blackColor];
    view1.alpha=0.6;
    [_viewPreview addSubview:view1];
    UIView* view2=[[UIView alloc]initWithFrame:CGRectMake(ScreenWidth*705/750, ScreenHeight*440/1334, ScreenWidth, ScreenHeight)];
    view2.backgroundColor=[UIColor blackColor];
    view2.alpha=0.6;
    [_viewPreview addSubview:view2];
    UIView* view3=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight*440/1334, ScreenWidth*45/750, ScreenHeight)];
    view3.backgroundColor=[UIColor blackColor];
    view3.alpha=0.6;
    [_viewPreview addSubview:view3];
    UIView* view4=[[UIView alloc]initWithFrame:CGRectMake(ScreenWidth*45/750,ScreenHeight*900/1334, ScreenWidth*660/750, ScreenHeight)];
    view4.backgroundColor=[UIColor blackColor];
    view4.alpha=0.6;
    [_viewPreview addSubview:view4];
    
    
    _viewPreview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"saoyisao(1).png"]];
    _viewPreview1.frame = CGRectMake(ScreenWidth*50/750, ScreenHeight*440/1334,ScreenWidth*700/750,ScreenHeight*430/1334);
    [_viewPreview addSubview:_viewPreview1];
    
    [_line removeFromSuperview];
    _line = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth*30/750, 0, ScreenWidth*600/750, 3)];
    _line.backgroundColor=[UIColor colorWithRed:160/255.0 green:224/255.0 blue:57/255.0 alpha:1.0];
    [_viewPreview1 addSubview:_line];
    
    
    
    d1=ScreenWidth*460/750;
    NSLog(@"ddddd%d",d1);
    if (d1==253) {
        d1=252;
    }
    if (d1==203) {
        d1=300;
    }
    
    if (d1==471) {
        d1=350;
    }
    NSLog(@"ddddd%d",d1);
    
  
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*30/750,ScreenHeight*420/1334, ScreenWidth*65/750, 1)];
    line1.backgroundColor = luseColor;
    [_viewPreview addSubview:line1];
    
    UILabel *line11 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*30/750,ScreenHeight*420/1334,1,ScreenWidth*65/750)];
    line11.backgroundColor = luseColor;
    [_viewPreview addSubview:line11];
    UILabel *line12 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*655/750,ScreenHeight*420/1334, ScreenWidth*65/750, 1)];
    line12.backgroundColor = luseColor;
    [_viewPreview addSubview:line12];
    UILabel *line13 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*720/750,ScreenHeight*420/1334, 1, ScreenWidth*65/750)];
    line13.backgroundColor = luseColor;
    [_viewPreview addSubview:line13];

    UILabel *line1111 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*30/750,_viewPreview1.bottom+ScreenHeight*50/1334, ScreenWidth*65/750, 1)];
    line1111.backgroundColor = luseColor;
    [_viewPreview addSubview:line1111];
    UILabel *line113 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*655/750,_viewPreview1.bottom+ScreenHeight*50/1334, ScreenWidth*65/750,1 )];
    line113.backgroundColor = luseColor;
    [_viewPreview addSubview:line113];

    UILabel *line112 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*720/750,line1111.bottom-ScreenWidth*65/750, 1,ScreenWidth*65/750)];
    line112.backgroundColor = luseColor;
    [_viewPreview addSubview:line112];
    
    UILabel *line111 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth*30/750,line1111.bottom-ScreenWidth*65/750, 1,ScreenWidth*65/750)];
    line111.backgroundColor = luseColor;
    [_viewPreview addSubview:line111];
   
    
    timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(lineAnimation) userInfo:nil repeats:YES];
    
     [self jiazaititle];

}
-(void)jiazaidi{
    [diview removeFromSuperview];
    [diviewTming removeFromSuperview];
    diview=[[UIView alloc]initWithFrame:CGRectMake(0,   ScreenHeight-64, ScreenWidth, 64)];
    
    diview.backgroundColor=[UIColor blackColor];
    diview.alpha=0.66;
    [self.view addSubview:diview];
    diviewTming=[[UIView alloc]initWithFrame:CGRectMake(0,   ScreenHeight-64, ScreenWidth, 64)];
    
    diviewTming.backgroundColor=[UIColor clearColor];
    [self.view addSubview:diviewTming];
    
       for (int i = 0; i<3; i++) {
        UIButton *nameButcenter = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   0, ScreenWidth/3, 50)];
        [nameButcenter addTarget:self action:@selector(hexiaoquan:) forControlEvents:UIControlEventTouchUpInside];
        [diviewTming addSubview:nameButcenter];
        nameButcenter.tag=i;
        
        UIButton *nameBut = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   45, ScreenWidth/3, 12)];
        [nameBut setTitle: [nameBtn objectAtIndex:i]  forState:UIControlStateNormal];
        nameBut.tag=i;
        [nameBut setTitleColor:shenhuicolor forState:0];
        
        if (i==commdity.sat.intValue) {
            [nameBut setTitleColor:luseColor forState:0];
        }
        nameBut.titleLabel.font=[UIFont systemFontOfSize:12];
        [nameBut addTarget:self action:@selector(hexiaoquan:) forControlEvents:UIControlEventTouchUpInside];
        
        [diviewTming addSubview:nameBut];
       
        UIButton *wuyebuttonimage = [UIButton buttonWithType:UIButtonTypeCustom];
        wuyebuttonimage.frame = CGRectMake(ScreenWidth/3*i, 10, 30, 30);
        wuyebuttonimage.tag =i;
        wuyebuttonimage.center=nameButcenter.center;
        [wuyebuttonimage setBackgroundImage:[UIImage imageNamed: [arrayimage objectAtIndex:i]] forState:UIControlStateNormal];
        if (i==commdity.sat.intValue) {
        [wuyebuttonimage setBackgroundImage:[UIImage imageNamed: [arrayimage1 objectAtIndex:i]] forState:UIControlStateNormal];
        }
        [wuyebuttonimage addTarget:self action:@selector(hexiaoquan:) forControlEvents:UIControlEventTouchUpInside];
        [diviewTming addSubview:wuyebuttonimage];
    }
    
    

    
    
}

-(void)shuliang{
    if ([commdity.sat isEqualToString:@"1"]) {

    [SCANSTOCK removeFromSuperview];
    [SCANSTOCK1 removeFromSuperview];
    [shuSCANSTOCK1 removeFromSuperview];

    
    SCANSTOCK = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*290/750, ScreenHeight*980/1334, ScreenWidth* 160/750, ScreenWidth* 160/750)];
    SCANSTOCK.layer.masksToBounds = YES;
    SCANSTOCK.layer.cornerRadius = ScreenWidth* 160/750/2;
    SCANSTOCK.layer.borderColor = luseColor.CGColor;
    SCANSTOCK.layer.borderWidth = 0.5;
    SCANSTOCK.contentMode = UIViewContentModeScaleAspectFit;
    
    SCANSTOCK1 =[[UIButton alloc]initWithFrame:CGRectMake(290*ScreenWidth/750, ScreenHeight*1010/1334,ScreenWidth* 160/750, ScreenWidth* 160/750)];
    [SCANSTOCK1  setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    SCANSTOCK1.titleLabel.font = [UIFont systemFontOfSize:13];
    [SCANSTOCK1 setTitle:@"已选商品" forState:UIControlStateNormal];
    SCANSTOCK1.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    shuSCANSTOCK1 =[[UIButton alloc]initWithFrame:CGRectMake(290*ScreenWidth/750, ScreenHeight*950/1334,ScreenWidth* 160/750, ScreenWidth* 160/750)];
    [shuSCANSTOCK1  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    shuSCANSTOCK1.titleLabel.font = [UIFont systemFontOfSize:24];
    shuSCANSTOCK1.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    
    [shuSCANSTOCK1 addTarget:self action:@selector(scanStockCountBtnClick) forControlEvents:UIControlEventTouchUpInside];
    NSNumber *sum = [howArray valueForKeyPath:@"@sum.floatValue"];
    
    [shuSCANSTOCK1 setTitle: [NSString stringWithFormat:@"%@", sum] forState:UIControlStateNormal];
    [self.view addSubview:SCANSTOCK];
    [self.view addSubview:SCANSTOCK1];
    [self.view addSubview:shuSCANSTOCK1];

    }
    
}
-(void)OnTapDone{
    
    [searchTextField resignFirstResponder];
    if ([commdity.sat isEqualToString:@"0"]) {
//        commdity.memberId=searchTextField.text;
//        MembershipDetailsViewController*detailViewController = [[MembershipDetailsViewController alloc] init];
//        
//        
//        [self.navigationController pushViewController:detailViewController animated:YES];
    }
    if ([commdity.sat isEqualToString:@"2"]) {
        OrderDetailViewController *detailViewController = [[OrderDetailViewController alloc] init];
        commdity.orderId=searchTextField.text;
        [self.navigationController pushViewController:detailViewController animated:YES];
    }
    if ([commdity.sat isEqualToString:@"1"]) {
        
        if (![scanStockResultArray containsObject:searchTextField.text]) {
            
            stringValue = searchTextField.text;
          
            [self performSelectorOnMainThread:@selector(loadStockVC) withObject:nil waitUntilDone:NO];
        }else{
            
            
        }
        
    }
    
    
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
-(void)sou{
    
    
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (![device hasTorch]) {
        NSLog(@"no torch");
    }else{
        [device lockForConfiguration:nil];
        no=!no;
        if (no) {
            [device setTorchMode: AVCaptureTorchModeOn];
            
            [sxBtn setImage:[UIImage imageNamed:@"shanguangdeng_hight"] forState:UIControlStateNormal];
            
        }
        else
        {
            [device setTorchMode: AVCaptureTorchModeOff];
            [sxBtn setImage:[UIImage imageNamed:@"shanguangdeng_normal"] forState:UIControlStateNormal];
        }
        
        
        [device unlockForConfiguration];
    }
    
    
}

-(void)hexiaoquan:(UIButton*)sender{
    
   
    if (sender.tag==0) {
        [self showAlertMessage:@"未开通"];
    
  
//    [SCANSTOCK removeFromSuperview];
//    [SCANSTOCK1 removeFromSuperview];
//    [shuSCANSTOCK1 removeFromSuperview];
//    commdity.sat=@"0";
//     [self jiazaidi];
}
    
   
    
    if (sender.tag==1) {
        
        
        commdity.sat=@"1";
        [self shuliang];
       
        [self jiazaidi];
    }

    if (sender.tag==2) {
        
        
        
        [SCANSTOCK removeFromSuperview];
        [SCANSTOCK1 removeFromSuperview];
        [shuSCANSTOCK1 removeFromSuperview];
        commdity.sat=@"2";
        [self jiazaidi];
    }
}


-(void)lineAnimation
{
    
    
    if (isDown==NO) {
        num++;
        _line.frame=CGRectMake(ScreenWidth*30/750, 2*num, ScreenWidth*600/750, 3);
        if (2*num==d1) {
            isDown=YES;
        }
    }
    else
    {
        
        
        num--;
        _line.frame=CGRectMake(ScreenWidth*30/750, 2*num, ScreenWidth*600/750, 3);
        if (num==0) {
            isDown=NO;
        }
    }
    
}

-(BOOL)startRead1
{
    NSError *error;
    //    [_outputsetRectOfInterest:CGRectMake((124)/ScreenHigh,((ScreenWidth-220)/2)/ScreenWidth,220/ScreenHigh,220/ScreenWidth)];
    AVCaptureDevice *captureDevice=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input=[AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
        NSLog(@"%@", [error localizedDescription]);
        return NO;
    }
    _captureSession = [[AVCaptureSession alloc] init];
    if ([_captureSession canAddInput:input]) {
        [_captureSession addInput:input];
    }
    
    
    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    if ([_captureSession canAddOutput:captureMetadataOutput]) {
        [_captureSession addOutput:captureMetadataOutput];
    }
    
    
    dispatch_queue_t dispatchQueue;
    dispatchQueue = dispatch_queue_create("myQueue", NULL);
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    captureMetadataOutput.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
    
    [captureMetadataOutput setRectOfInterest:CGRectMake((ScreenWidth*0.475)/ScreenHeight,((ScreenWidth-ScreenHeight*0.475)/2)/ScreenWidth,ScreenHeight*0.475/ScreenHeight,ScreenHeight*0.400/ScreenWidth)];
    
    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [_videoPreviewLayer setFrame:CGRectMake(0, 0,ScreenWidth,ScreenHeight)];
    [_viewPreview.layer addSublayer:_videoPreviewLayer];
    [_captureSession startRunning];
    return YES;
}





-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    if (metadataObjects != nil && [metadataObjects count] > 0) {
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
        
        if ([commdity.sat isEqualToString:@"2"]) {
            
            [_captureSession stopRunning];
            [_videoPreviewLayer removeFromSuperlayer];
            _captureSession = nil;
            [timer invalidate];
            timer=nil;
            [self performSelectorOnMainThread:@selector(loadShopVC) withObject:nil waitUntilDone:NO];
            
            
            
        }
        
        if ([commdity.sat isEqualToString:@"1"]) {
            [self tingdun];
           [self performSelectorOnMainThread:@selector(loadStockVC) withObject:nil waitUntilDone:NO];
            
        }
        
        if ([commdity.sat isEqualToString:@"0"]) {
            [_captureSession stopRunning];
            //           [_videoPreviewLayer removeFromSuperlayer];
            _captureSession = nil;
            [timer invalidate];
            timer=nil;

            
            [self performSelectorOnMainThread:@selector(hequan) withObject:nil waitUntilDone:NO];
            
        }
        
    }
    
}
-(void)hequan
{
    
    
//    commdity.memberId=stringValue;
//    commdity.gengxin=@"";
//    NSLog(@"----%@", commdity.memberId);
//    MembershipDetailsViewController *detailViewController = [[MembershipDetailsViewController alloc] init];
//    [self.navigationController pushViewController:detailViewController animated:YES];

//    huiyuanDetailsViewController*detailViewController = [[huiyuanDetailsViewController alloc] init];
//    [self.navigationController pushViewController:detailViewController animated:YES];
    
}
-(void)tingdun{

    [_captureSession stopRunning];
    _captureSession = nil;
    [timer invalidate];
    timer=nil;


}
-(void)zaisao{
    
    [self performSelector:@selector(loadStockVCr) withObject:nil afterDelay:2.0f];
//    [self jiazaititle];
    
}

-(void)loadStockVC
{
    
    if (![scanStockResultArray containsObject:stringValue]) {
        

        [self requestItemDetail:stringValue];//不一样的
 
    }else{

            [self zaisao];

    }
}
-(void)loadStockVCr{
    
    if (howArray .count>0) {
        
    
    for (int i=0; i<scanStockResultArray.count; i++) {
        
        NSString *codeStr = [scanStockResultArray objectAtIndex:i];
        NSString *how = [howArray objectAtIndex:i];
        if ([codeStr isEqualToString:stringValue] ) {
            
    
            double howd;
            howd=[how doubleValue];
            howd++;

            [howArray replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%.f", howd]];

      
        }
    }
    }
    [self add];
}

//放到对应的数组
-(void)requestItemDetail:(NSString *)codeStr
{
    
    if (![scanStockResultArray containsObject:codeStr]) {
        NSLog(@"stringValue===%@",codeStr);
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:codeStr forKey:@"pid"];
        
        NSLog(@"====%@",params);
        
        [HttpRequest requestData:@"Product/Detail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            
            if (isSuccess) {
                
                NSLog(@"====%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
          
                    NSMutableArray*specList=[severDataDict.dataDict objectForKey:@"specList"];
                   
                    if (specList.count>0) {
                        
                    
                        for (int i=0; i<specList.count; i++) {
                            
                            NSString *codeStr = [[specList objectAtIndex:i]objectForKey:@"specId"];
                            
                            if ([codeStr isEqualToString:[severDataDict.dataDict objectForKey:@"currentSpecId"]] ) {
                                
                                specName=[specList[i]objectForKey:@"specName"];
                                
                            }
                        }
                   
                    }else{
                        
                        specName=@"";
                        
                }
                   
                    
                    if ([[severDataDict.dataDict objectForKey:@"currentSpecId"]isEqualToString:@""]) {
                         specName=@"";
                    }
                        
                        [self setScanStockStringValue:stringValue urlStr:[[severDataDict.dataDict objectForKey:@"imageList"][0] objectForKey:@"imageUrlFull"] typeStr:[severDataDict.dataDict objectForKey:@"itemType"]  priceStr:[severDataDict.dataDict objectForKey:@"salePrice"]  itemIdStr:[severDataDict.dataDict objectForKey:@"itemId"] itemNameStr:[severDataDict.dataDict objectForKey:@"itemName"]  currentSpecId:[severDataDict.dataDict objectForKey:@"currentSpecId"] specNameStr:specName  ItemCodeStr:[severDataDict.dataDict objectForKey:@"itemCode"] ];

                }else
                {
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                     [self zaisao];
                    
                }
            }
            else
            {
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
    }
}
-(void)setScanStockStringValue:(NSString *)str urlStr:(NSString *)urlStr typeStr:(NSString *)typeStr  priceStr:(NSString *)priceStr itemIdStr:(NSString *)itemIdStr  itemNameStr:(NSString *)itemNameStr currentSpecId:(NSString *)currentSpecId specNameStr:(NSString *)specNameStr ItemCodeStr:(NSString *)ItemCodeStr

{
    if (![scanStockResultArray containsObject:str]) {
        stringValue=@"";
        [scanStockResultArray addObject:str];
        [itemUrlArray addObject:urlStr];
        [itemPriceArray addObject:priceStr];
        [itemNameArray addObject:itemNameStr];
        [itemidArray addObject:itemIdStr];
        [itemCodeArray addObject:ItemCodeStr];
        
        [currentSpecIdArray addObject:currentSpecId];
        [specNameArray addObject:specNameStr];
        [howArray addObject:@"1"];
        commdity.how=@"123";
        
        NSNumber *sum = [howArray valueForKeyPath:@"@sum.floatValue"];
        
        [shuSCANSTOCK1 setTitle: [NSString stringWithFormat:@"%@", sum] forState:UIControlStateNormal];
        [shuSCANSTOCK1 addTarget:self action:@selector(scanStockCountBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self zaisao];
        
        
    }
}


-(void)loadShopVC
{

    OrderDetailViewController *detailViewController = [[OrderDetailViewController alloc] init];
    commdity.orderId=stringValue;
  
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}
-(void)scanStockCountBtnClick
{
    [_captureSession stopRunning];
    
    _captureSession = nil;
    [timer invalidate];
    timer=nil;
   
    NSLog(@"－－－－－－%@",commdity.ItemCode44);
    
    commdity.ItemCode44=itemCodeArray;
    commdity.ItemName44=itemNameArray;
    commdity.ImageUrl44=itemUrlArray;
    commdity.Itemid44=itemidArray;
    commdity.specName44=specNameArray;
    commdity.Price44=itemPriceArray;
    commdity.currentSpecId44=currentSpecIdArray;
    commdity.how44=howArray;
    kucunViewController*detailViewController = [[kucunViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];
    
   }




-(void)webVC
{
    

    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {

    }
}

-(IBAction)backBtnClick:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)stopReading{
    [_captureSession stopRunning];
    _captureSession = nil;
    
    [_videoPreviewLayer removeFromSuperlayer];
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:3.0f];
    
}


- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:5.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
